package org.standard.sys.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.standard.base.model.BaseUser;
import org.standard.commons.struts2.BaseAction;
import org.standard.commons.utils.Constants;
import org.standard.commons.utils.DateUtil;
import org.standard.commons.utils.FlipPageInfo;
import org.standard.sys.model.SysCode;
import org.standard.sys.model.SysLog;
import org.standard.sys.service.SysLogService;

public class SysLogAction extends BaseAction {
	private SysLogService sysLogService;
	private List<SysLog> sysLogs;
	private List<SysCode> codes;
	private Integer totleDay;
	private Integer totleRecord;
	private Integer yearRecord;
	private Integer monthRecord; 
	private Integer dayRecord;
	private Integer aveRecord;
	private String  logType;
	private Date startTime;
	private Date endTime;
	private String logIp;
	private String searchType;
	private String codeno;
	private FlipPageInfo fpi=new FlipPageInfo();
	private String endtimestr;
	private BaseUser user;
	private String[] sellog;
	private String sstart;
	private String send;
	
	/**
	 * 在查询后根据所选择的日志记录进行删除
	 * @return
	 */
	public String deletesysLog()
	{
        char s=sysLogService.deleteLog(sellog);
        if('1'==s){
        	if(null!=sstart&&!"".equals(sstart))
        		setStartTime(new Date(sstart));
        	if(null!=send&&!"".equals(send))
        		setEndTime(new Date(send));
			return searchLog();
            }
		else{
			info="删除失败";
	    	return "blank";
	    	}
	}
	
	/**
	 * 返回日志概况界面
	 * @return
	 */
	public String summary()
	{
		List templist=sysLogService.summary();
		totleDay=(Integer)templist.get(0); //获得总天数
		totleRecord=(Integer)templist.get(1);//获得总记录数
		yearRecord=(Integer)templist.get(2);//获得年记录数
		monthRecord=(Integer)templist.get(3);//获得与记录数
		dayRecord=(Integer)templist.get(4);//获得日记录数
		aveRecord=(Integer)templist.get(5);//获得平均记录
		fpi.setSize(10);
		fpi=sysLogService.recentRecord(fpi);//获得最近10条记录
		return "summary";
	}
	
	
	/**
	 * 返回日志概况界面
	 * @return
	 */
	public String index()
	{
        codes=sysLogService.searchLogType();
        Date date=new Date();//今天日期
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //日期格式
        endtimestr=bartDateFormat.format(date); //指定一个字符串
		return "admin";
	}
	
	/**
	 * 返回日志查询结果
	 * @return
	 */
	public String searchLog()
	{
		if("0".equals(searchType))//查询日志
		{
			if(startTime!=null&&!"".equals(startTime)&&endTime!=null&&!"".equals(endTime))//如果开始时间和结束时间同时不为空的话
			{
				if(DateUtil.beforeDays(endTime,startTime)>0)//如果开始时间大于结束时间，返回
				{
					info="开始时间不可大于结束时间";
					return "blank";
				}
			}
		fpi.setSize(300);//设置显示的记录数
		fpi=sysLogService.searchLog(fpi,codeno, startTime, endTime, logIp);
		return "searchLog";
		}
		else   //删除日志
		{
			if(startTime!=null&&!"".equals(startTime)&&endTime!=null&&!"".equals(endTime))//如果开始时间和结束时间同时不为空的话
			{
				if(DateUtil.beforeDays(endTime,startTime)>0)//如果开始时间大于结束时间，返回
				{
					info="开始时间不可大于结束时间";
					return "blank";
				}
			}
		char s=sysLogService.deleteLog(codeno, startTime, endTime, logIp);
		if('1'==s)
			info="删除成功";
		else
			info="删除失败";
		return "blank";
		}
	}
	
	
	/**
	 * 返回日志概况界面
	 * @return
	 */
	public String deleteAllLog()
	{
        char s=sysLogService.deleteAllLog();
        if('1'==s)
			info="清空成功";
		else
			info="清空失败";
		return "blank";
	}
	
	
	
	
	

	/**
	 * 控制面板中，安全日志，显示个人的最近10条日志
	 * @return日志列表
	 */
	public String searchUserLog()
	{
		//获得用户身份
		user = (BaseUser)request.getSession().getAttribute(Constants.SESSION_USER);
		fpi.setSize(10);//设置显示的记录数
		fpi=sysLogService.recentRecord(fpi,user.getUserId());
		return "userLog";
	}
	
	
	public SysLogService getSysLogService() {
		return sysLogService;
	}
	public void setSysLogService(SysLogService sysLogService) {
		this.sysLogService = sysLogService;
	}
	public Integer getTotleDay() {
		return totleDay;
	}
	public void setTotleDay(Integer totleDay) {
		this.totleDay = totleDay;
	}
	public Integer getTotleRecord() {
		return totleRecord;
	}
	public void setTotleRecord(Integer totleRecord) {
		this.totleRecord = totleRecord;
	}
	public Integer getYearRecord() {
		return yearRecord;
	}
	public void setYearRecord(Integer yearRecord) {
		this.yearRecord = yearRecord;
	}
	public Integer getMonthRecord() {
		return monthRecord;
	}
	public void setMonthRecord(Integer monthRecord) {
		this.monthRecord = monthRecord;
	}
	public Integer getDayRecord() {
		return dayRecord;
	}
	public void setDayRecord(Integer dayRecord) {
		this.dayRecord = dayRecord;
	}
	public Integer getAveRecord() {
		return aveRecord;
	}
	public void setAveRecord(Integer aveRecord) {
		this.aveRecord = aveRecord;
	}

	public List<SysLog> getSysLogs() {
		return sysLogs;
	}

	public void setSysLogs(List<SysLog> sysLogs) {
		this.sysLogs = sysLogs;
	}

	public List<SysCode> getCodes() {
		return codes;
	}

	public void setCodes(List<SysCode> codes) {
		this.codes = codes;
	}

	public String getLogType() {
		return logType;
	}

	public void setLogType(String logType) {
		this.logType = logType;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public String getLogIp() {
		return logIp;
	}

	public void setLogIp(String logIp) {
		this.logIp = logIp;
	}


	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public String getCodeno() {
		return codeno;
	}

	public void setCodeno(String codeno) {
		this.codeno = codeno;
	}


	public FlipPageInfo getFpi() {
		return fpi;
	}


	public void setFpi(FlipPageInfo fpi) {
		this.fpi = fpi;
	}


	public String getEndtimestr() {
		return endtimestr;
	}


	public void setEndtimestr(String endtimestr) {
		this.endtimestr = endtimestr;
	}


	public BaseUser getUser() {
		return user;
	}


	public void setUser(BaseUser user) {
		this.user = user;
	}


	public String[] getSellog() {
		return sellog;
	}


	public void setSellog(String[] sellog) {
		this.sellog = sellog;
	}

	public String getSstart() {
		return sstart;
	}

	public void setSstart(String sstart) {
		this.sstart = sstart;
	}

	public String getSend() {
		return send;
	}

	public void setSend(String send) {
		this.send = send;
	}


}
